package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Goods;
import com.weipai.springboot.model.IntegralGoods;
import com.weipai.springboot.model.ProductDesc;
import com.weipai.springboot.model.SpecialOffer;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.IntegralGoodsDao;
import com.weipai.springboot.module.mapper.GoodsMapper;
import com.weipai.springboot.module.mapper.IntegralGoodsMapper;
import com.weipai.springboot.module.mapper.ProductDescMapper;
import com.weipai.springboot.module.service.IntegralGoodsService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/03 11:29
 */
@Service
public class IntegralGoodsServiceImpl implements IntegralGoodsService {

    private static final Logger log = LoggerFactory.getLogger(IntegralGoodsServiceImpl.class);

    @Autowired
    IntegralGoodsDao integralGoodsDao;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    ProductDescMapper productDescMapper;

    //保存
    @Override
    public Response save(IntegralGoods integralGoods) {
        Response response = new Response();
        IntegralGoods goods = integralGoodsDao.selectByGoodsId(integralGoods.getGoodsId());
        if(null==goods){
            if(1==integralGoodsDao.insert(integralGoods)){
                response.setCode(RetCode.SUCCESS);
            }else {
                log.debug("保存品牌失败:{}",integralGoods.getIntegralGoodsId());
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else{
            response.put("code","401");
            response.put("msg","此商品已是积分商品，请勿重复添加");
        }
        return response;
    }

    //查询积分商品详情
    @Override
    public Response getIntegralGoodsById(String integralGoodsId) {
        Response response = new Response();
        //此处应查询出此商品的名称和分类进行封装返回  ------>????
       IntegralGoods integralGoods = integralGoodsDao.getIntegralGoodsById(integralGoodsId);
       Goods goods = goodsMapper.selectByPrimaryKey(integralGoods.getGoodsId());
       ProductDesc productDesc = productDescMapper.selectBySpuNumber(goods.getSpuNumber());
        if(integralGoods!= null  ){
            response.setCode(RetCode.SUCCESS);
            response.put("integralGoods",integralGoods);
            response.put("goods",goods);
            response.put("productDesc",productDesc);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //删除（逻辑删除）
    @Override
    public Response deleteById(String integralGoodsId) {
        Response response = new Response();
        if(1==integralGoodsDao.updateDel(integralGoodsId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //更改积分商品的状态
    @Override
    public Response changeStatus(String integralGoodsId) {
        Response response = new Response();
        IntegralGoods integralGoods = integralGoodsDao.getIntegralGoodsById(integralGoodsId);
        if(null!=integralGoods){
            integralGoods.setUpdateTime(DateUtils.timeFormat(System.currentTimeMillis()));
            if(integralGoods.getIntegralGoodsStatus()==0){
                integralGoods.setIntegralGoodsStatus(-1);
            }else{
                integralGoods.setIntegralGoodsStatus(0);
            }
            if(1==integralGoodsDao.updateByPrimaryKey(integralGoods)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(integralGoodsDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //分页查询
    @Override
    public Response selectAllByPage(Integer page, Integer size, String draw) {
        Response response = new Response();
        Integer start = (page-1) * size;

        if(draw.equals("app")){
            List<Map<String, Object>> list = integralGoodsDao.selectAllByPage22(start, size);
            Integer totalNum = integralGoodsDao.selectAllByPageAmount22();
            PageResult<Map<String, Object>> pageList = new PageResult<>(list,page,size,totalNum, draw);
            response = new Response();
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
        }else{
            List<Map<String, Object>> list = integralGoodsDao.selectAllByPage(start, size);
            Integer totalNum = integralGoodsDao.selectAllByPageAmount();
            PageResult<Map<String, Object>> pageList = new PageResult<>(list,page,size,totalNum, draw);
             response = new Response();
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
        }

        return response;
    }

    @Override
    public Response update(IntegralGoods integralGoods) {
        Response response = new Response();
        if(1==integralGoodsDao.updateByPrimaryKeySelective22(integralGoods)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;

    }
}
